﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZK.MyControl;

namespace ZK.Client
{
    public class FormHelper
    {

        private static Dictionary<string, Button> keyValuePairsButton = new Dictionary<string, Button>();

        private static Dictionary<string, Form> keyValuePairsForm = new Dictionary<string, Form>();

        private static Dictionary<string, Type> keyValuePairsType = new Dictionary<string, Type>();


        public static Form CurrentForm { get; set; }
        public static Button CurrentButton { get; set; }
        public static Panel BtnPanel { get; set; }
        public static Panel FormPanel { get; set; }

        /// <summary>
        /// 初始化窗体
        /// </summary>

        public static void Init(List<Button> buttons, Panel formPanel, List<Type> typeList)
        {

 
            keyValuePairsType.Clear();
            FormPanel = formPanel;
            foreach (Type type in typeList)
            {
                keyValuePairsType.Add(type.Name, type);
                try
                {
                    Form objFrm = (Form)Activator.CreateInstance(type);
                    if (objFrm == null)
                    {
                        return;
                    }
                    else
                    {
                        objFrm.TopLevel = false; //将子窗体设置成非最高层，非顶级控件
                        objFrm.WindowState = FormWindowState.Maximized;//将当前窗口设置成最大化
                        objFrm.FormBorderStyle = FormBorderStyle.None;//去掉窗体边框
                        objFrm.Parent = FormPanel;//指定子窗体显示的容器

                        keyValuePairsForm.Add(type.Name, objFrm);
                    }
                    Button button = buttons[typeList.IndexOf(type)];
                    button.Click += Btn_Click;
                    button.Tag = type.Name;
                    keyValuePairsButton.Add(type.Name, button);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }

            }
            //BtnPanel = btnPanel;
            //FormPanel = formPanel;
            //keyValuePairsButton.Clear();

            //keyValuePairsType.Clear();
            //foreach (Type type in typeList)
            //{
            //    keyValuePairsType.Add(type.Name, type);

            //    try
            //    {
            //        Form objFrm = (Form)Activator.CreateInstance(type);
            //        if (objFrm == null)
            //        {
            //            return;
            //        }
            //        else
            //        {
            //            objFrm.TopLevel = false; //将子窗体设置成非最高层，非顶级控件
            //            objFrm.WindowState = FormWindowState.Maximized;//将当前窗口设置成最大化
            //            objFrm.FormBorderStyle = FormBorderStyle.None;//去掉窗体边框
            //            objFrm.Parent = FormPanel;//指定子窗体显示的容器

            //            keyValuePairsForm.Add(type.Name, objFrm);
            //        }

            //        ButtonEx btn = new ButtonEx();
            //        btn.Text =  objFrm.Text ;

            //        btn.Radius = 15;
            //        btn.RoundStyle = RoundStyle.All;
            //        btn.BaseColor = System.Drawing.Color.FromArgb(40, 132, 194);


            //        btn.Font = new System.Drawing.Font("宋体", 18, System.Drawing.FontStyle.Bold);
            //        btn.Dock = DockStyle.Left;
            //        btn.Height = 65;
            //        btn.FlatStyle = FlatStyle.Popup;
            //        btn.ForeColor = System.Drawing.Color.White;
            //        btn.Click += Btn_Click;
            //        btn.Tag = type.Name;
            //        BtnPanel.Controls.Add(btn);
            //        btn.BringToFront();
            //        keyValuePairsButton.Add(type.Name, btn);

            //        Panel panel = new Panel();
            //        panel.Height = 3;
            //        panel.Dock = DockStyle.Top;
            //        BtnPanel.Controls.Add(panel);
            //        panel.BringToFront();

            //    }
            //    catch (Exception ex)
            //    {
            //        MessageBox.Show(ex.Message);
            //        return;
            //    }

            //}

        }


        private static void Btn_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;


            LoadForm(keyValuePairsType[btn.Tag.ToString()]);

        }

        public static void LoadForm(Type type)
        {

            ClosePreForm();

            if (keyValuePairsForm.Keys.Contains(type.Name))
            {

                CurrentForm = keyValuePairsForm[type.Name];

                Button btn = keyValuePairsButton[type.Name];
                if (CurrentButton != null)
                    CurrentButton.ForeColor = System.Drawing.Color.White;
                CurrentButton = btn;
                CurrentButton.ForeColor = System.Drawing.Color.Black;

                keyValuePairsForm[type.Name].Show();
            }
            //else
            //{

            //    MessageBox.Show(string.Format("未加载初始化窗体【{0}】", type.Name));
            //}
        }

        public static void ClosePreForm()
        {
            if (FormHelper.CurrentForm != null)
            {
                FormHelper.CurrentForm.Hide();
            }
        }




    }
}